package cn.ltgodm.house.controller;

import cn.ltgodm.house.domain.*;
import cn.ltgodm.house.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ltgodm
 * @date 2024-06-25 11:43:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/house")
public class AnalysisResultController {
    private final DistrictPriceService districtPriceService;
    private final DecorationPriceService decorationPriceService;
    private final BuildingTypeCountService buildingTypeCountService;
    private final HouseTypeCountService houseTypeCountService;
    private final AreaAttentionPriceService areaAttentionPriceService;
    private final HouseElevatorPriceService houseElevatorPriceService;
    private final FloorPriceService floorPriceService;
    private final ElevatorCountPriceService elevatorCountPriceService;

    @GetMapping("/district")
    public ResultMap districtPriceAnalysis() {
        List<DistrictPrice> districtPriceList = districtPriceService.list();
        Map<String, Object> result = new HashMap<>();
        List<ResultVo> dataList = districtPriceList.stream().map(item -> new ResultVo(item.getDistrict(), item.getPrice().toString())).collect(Collectors.toList());
        result.put("dataList", dataList);
        result.put("regionCode", "hangzhou");
        return ResultMap.success(result);
    }

    @GetMapping("/decorate")
    public ResultMap decoratePriceAnalysis() {
        List<DecorationPrice> list = decorationPriceService.list();
        return ResultMap.success(list);
    }

    @GetMapping("/building_type")
    public ResultMap buildingTypePriceAnalysis() {
        return ResultMap.success(buildingTypeCountService.list());
    }

    @GetMapping("/house_type")
    public ResultMap houseTypePriceAnalysis(Integer limitNum) {
        return ResultMap.success(houseTypeCountService.list(new LambdaQueryWrapper<HouseTypeCount>().orderByDesc(HouseTypeCount::getPrice).last("limit " + limitNum)));
    }

    @GetMapping("/area")
    public ResultMap areaPriceAnalysis() {
        List<AreaAttentionPrice> list = areaAttentionPriceService.list(new LambdaQueryWrapper<AreaAttentionPrice>().orderByAsc(AreaAttentionPrice::getMinPrice));
        Map<String, Object> result = new HashMap<>();
        List<Double> barData = list.stream().map(AreaAttentionPrice::getInnerAreaAvg).collect(Collectors.toList());
        List<Double> lineData = list.stream().map(AreaAttentionPrice::getBuildingAreaAvg).collect(Collectors.toList());
        List<Double> rateData = list.stream().map(AreaAttentionPrice::getAttentionAvg).collect(Collectors.toList());
        List<String> category = list.stream().map(item -> item.getMinPrice() + "-" + item.getMaxPrice() + "万元").collect(Collectors.toList());
        result.put("barData", barData);
        result.put("lineData", lineData);
        result.put("rateData", rateData);
        result.put("category", category);
        return ResultMap.success(result);
    }

    //有无电梯价格分析
    @GetMapping("/have_elevator")
    public ResultMap elPriceAnalysis() {
        List<ElevatorCountPrice> list = elevatorCountPriceService.list(new LambdaQueryWrapper<ElevatorCountPrice>().orderByAsc(ElevatorCountPrice::getMinPrice));
        Map<String, Object> result = new HashMap<>();
        List<Long> barData = list.stream().map(ElevatorCountPrice::getElevatorCount).collect(Collectors.toList());
        List<Long> lineData = list.stream().map(ElevatorCountPrice::getNoElevatorCount).collect(Collectors.toList());
        List<String> category = list.stream().map(item -> item.getMinPrice() + "-" + item.getMaxPrice() + "万元").collect(Collectors.toList());
        result.put("numList", barData);
        result.put("numList2", lineData);
        result.put("dateList", category);
        return ResultMap.success(result);
    }

    //梯户价格比例分析
    @GetMapping("/elevator_house")
    public ResultMap elevatorHouseAnalysis() {
        List<HouseElevatorPrice> list = houseElevatorPriceService.list(new LambdaQueryWrapper<HouseElevatorPrice>()
                .orderByDesc(HouseElevatorPrice::getCount).last("limit 8"));
        List<ResultVo> result = list.stream().map(item -> new ResultVo(item.getElevator(), item.getPrice().toString())).sorted(Comparator.comparing(ResultVo::getValue).reversed()).collect(Collectors.toList());
        return ResultMap.success(result);
    }

    //楼层价格分析
    @GetMapping("/floor")
    public ResultMap floorPriceAnalysis(@RequestParam(defaultValue = "20") Integer limitNum) {
        List<FloorPrice> list = floorPriceService.list(new LambdaQueryWrapper<FloorPrice>()
                .orderByDesc(FloorPrice::getCount).last("limit "+limitNum));
        List<FloorPrice> result = list.stream().sorted(Comparator.comparing(FloorPrice::getPrice).reversed()).collect(Collectors.toList());
        return ResultMap.success(result);
    }





}
